import { createSeriesBarTopRectRenderItem } from '../BarChart/options';
import { BAR_WIDTH } from '../constants';
import { isNotData } from '@/utils/is';

type TCreateCustomSeriesOptions = {
  stoke: string;
  yAxisIndex?: number;
  data: number[];
  name?: string;
};
export default function useBarChart() {
  function createCustomSeries(list: TCreateCustomSeriesOptions[]) {
    return list.map((item, index) => {
      return {
        type: 'custom',
        data: item.data,
        name: item.name,
        yAxisIndex: isNotData(item.yAxisIndex) ? index : item.yAxisIndex,
        tooltip: {
          show: false
        },
        renderItem(params, api: any) {
          const highPoint = api.coord([api.value(0), api.value(1)]);
          if (list.length === 1) {
            const halfWidth = 6;

            return {
              type: 'line',
              transition: ['shape'],
              style: {
                lineWidth: 2,
                stroke: item.stoke
              },
              shape: {
                x1: highPoint[0] - halfWidth,
                y1: highPoint[1],
                x2: highPoint[0] + halfWidth,
                y2: highPoint[1]
              }
            };
          }

          const halfWidth = BAR_WIDTH / 2;
          return {
            type: 'line',
            transition: ['shape'],
            style: {
              lineWidth: 2,
              stroke: item.stoke
            },
            shape: {
              x1: index === 0 ? highPoint[0] - halfWidth * 2 : highPoint[0],
              y1: highPoint[1],
              x2: index === 0 ? highPoint[0] : highPoint[0] + halfWidth * 2,
              y2: highPoint[1]
            }
          };
        }
      } as echarts.EChartOption.SeriesCustom;
    });
  }

  return {
    createCustomSeries,
    createSeriesBarTopRectRenderItem
  };
}
